package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CompanyFroumMyAnswerAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CompanyForumMyAnswer;
import com.isunland.managesystem.entity.CompanyForumMyAnswer.MyAnswerDetail;
import com.isunland.managesystem.entity.CompanyForumTopic;
import com.isunland.managesystem.entity.CompanyForumTopic.actualObject;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 我发表的评论
 *
 * @author wu
 */
public class KnowledgeForumMyReplyListFragment extends BaseListFragment {

  private CurrentUser mCurrentUser;
  private String mRegetStaffId;
  private CompanyFroumMyAnswerAdapter mAdapter;
  private String mMainId;
  private BaseVolleyActivity mActivity;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mActivity = (BaseVolleyActivity) getActivity();
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.myAnswerForum);
    mCurrentUser = CurrentUser.newInstance(getActivity());
    mRegetStaffId = mCurrentUser.getJobNumber();
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_KNOWLEDGEFORUM_REPLY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("regStaffId", mRegetStaffId);
    params.put("app", "app");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    CompanyForumMyAnswer myAnswer = new Gson().fromJson(arg0, CompanyForumMyAnswer.class);
    List<MyAnswerDetail> answerDetail = myAnswer.getData();
    setAdapter(answerDetail);
  }

  private void setAdapter(List<MyAnswerDetail> answerDetail) {
    if (answerDetail == null) {
      answerDetail = new ArrayList<MyAnswerDetail>();
    }

    if (mAdapter == null) {
      mAdapter = new CompanyFroumMyAnswerAdapter(getActivity(), answerDetail);
      setListAdapter(mAdapter);
      return;
    }
    ((CompanyFroumMyAnswerAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    MyAnswerDetail answerDetail = mAdapter.getItem(position - 1);
    mMainId = answerDetail.getMainId();
    getCommentTopic();// 获取评论对应的话题
    super.onListItemClick(l, v, position, id);
  }

  private void getCommentTopic() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_KNOWLEDGEFORUM_POST_LIST);
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("id", mMainId);
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) {
        CompanyForumTopic commentTopic = new Gson().fromJson(arg0, CompanyForumTopic.class);
        String omentTopic = commentTopic.getTotal();
        List<actualObject> topicDetail = commentTopic.getData();
        // 只有一个话题。
        if ("0".equals(omentTopic)) {
          Toast.makeText(getActivity(), R.string.notExistTopic, Toast.LENGTH_SHORT).show();
        } else {
          actualObject actualObject = topicDetail.get(0);
          Intent intent = new Intent(getActivity(), KnowledgeForumPostDetailAcrivity.class);
          intent.putExtra(KnowledgeForumPostDetailFragment.EXTRA_VALUE, actualObject);
          startActivity(intent);
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        LogUtil.e("VolleyError==" + arg0);
        Toast.makeText(getActivity(), R.string.dataAnyError, Toast.LENGTH_SHORT).show();
        // 错误提示。
      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  /**
   * 响应返回按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:
        //			if (NavUtils.getParentActivityName(getActivity()) != null) {
        //				NavUtils.navigateUpFromSameTask(getActivity());
        //			}
        getActivity().finish();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
